<template>
  <div class="grade">
    <!-- 搜索 -->
    <el-radio v-model="search_flag" label="1" border>班级搜索</el-radio>
    <el-radio v-model="search_flag" label="2" border>姓名搜索</el-radio>
    <div class="search_wrap">
      <el-input
        placeholder="通过班级搜索"
        v-show="search_flag == '1'"
        v-model="search"
        clearable
      >
      </el-input>
      <el-input
        placeholder="通过辅导员搜索"
        v-show="search_flag == '2'"
        v-model="search"
        clearable
      >
      </el-input>
      <el-button type="primary" icon="el-icon-search" @click="search_fn"
        >搜索</el-button
      >
      <el-button type="primary" icon="el-icon-edit">添加</el-button>
      <el-button
        type="primary"
        icon="el-icon-download"
        @click="getExcel"
        :loading="false"
        >导出</el-button
      >
    </div>

    <!-- 表格 -->
    <el-table v-loading="false" :data="pageTable" border style="width: 100%">
      <el-table-column prop="index" label="序号" width="55"></el-table-column>
      <el-table-column prop="s_number" label="学号" width="180"></el-table-column>
      <el-table-column prop="name" label="姓名" width="180"></el-table-column>
      <el-table-column prop="grade" label="班级" width="200"></el-table-column>
      <el-table-column prop="leaveBy" label="离校方式" width="200"></el-table-column>
      <el-table-column prop="backTime" label="预计返校时间" width="200"></el-table-column>
      <el-table-column prop="tel" label="联系电话" width="180"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="searchTable.length"
    >
    </el-pagination>
  </div>
</template>

<script>
import service from "../../utils/request"
export default {
  name:"Grade",
  data() {
    return {
      search_flag: "1", //搜索标识
      search: null, // 搜索框的值
      // 源数据
      tableData: [
        {
          index: 1,
          department: "计算机科学与工程学院",
          grade: "2018级计算机科学与技术二班",
          name: "小虎",
          tel: "17347644984",
        },
        {
          index: 2,
          department: "计算机科学与工程学院",
          grade: "2018级计算机科学与技术一班",
          name: "王小虎",
          tel: "17347644984",
        },
        {
          index: 3,
          department: "计算机科学与工程学院",
          grade: "2018级计算机科学与技术二班",
          name: "王小虎",
          tel: "17347644984",
        },
        {
          index: 4,
          department: "计算机科学与工程学院",
          grade: "2018级计算机科学与技术一班",
          name: "王小虎",
          tel: "17347644984",
        },
        {
          index: 5,
          department: "计算机科学与工程学院",
          grade: "2018级计算机科学与技术一班",
          name: "王小虎",
          tel: "17347644984",
        },
        {
          index: 6,
          department: "计算机科学与工程学院",
          grade: "2018级计算机科学与技术一班",
          name: "王小虎",
          tel: "17347644984",
        },
        {
          index: 7,
          department: "计算机科学与工程学院",
          grade: "2018级计算机科学与技术一班",
          name: "王小虎",
          tel: "17347644984",
        },
        {
          index: 8,
          department: "计算机科学与工程学院",
          grade: "2018级计算机科学与技术一班",
          name: "王小虎",
          tel: "17347644984",
        },
        {
          index: 9,
          department: "计算机科学与工程学院",
          grade: "2018级计算机科学与技术一班",
          name: "王小虎",
          tel: "17347644984",
        },
        {
          index: 10,
          department: "计算机科学与工程学院",
          grade: "2018级计算机科学与技术一班",
          name: "王小虎",
          tel: "17347644984",
        },
        {
          index: 11,
          department: "计算机科学与工程学院",
          grade: "2018级计算机科学与技术一班",
          name: "王小虎",
          tel: "17347644984",
        },
        {
          index: 12,
          department: "计算机科学与工程学院",
          grade: "2018级计算机科学与技术一班",
          name: "王小虎",
          tel: "17347644984",
        },
      ],
      page: 1,
      // 搜索筛选后的数据
      searchTable: [],
    };
  },
  computed: {
    // 对显示数据进行分页
    pageTable() {
      return this.searchTable.filter((item, index) => {
        return index < 10 * this.page && index >= 10 * (this.page - 1);
      });
    },
    // searchTable(){}
  },
  methods: {
    // 搜索函数
    search_fn() {
      // console.log(this.search)
      let key = this.search_flag == "1" ? "grade" : "name";
      // console.log(key)

      this.searchTable = this.tableData.filter((item, index) => {
        // console.log(item[key])
        return item[key].indexOf(this.search) != -1;
      });
      console.log(this.searchTable);
    },
    // 修改按钮回调
    handleEdit(index, row) {
      console.log(index, row);
    },
    // 删除按钮回调
    handleDelete(index, row) {
      console.log(index, row);
    },

    // 换页的回调
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      console.log(this.tableData.length);
    },
    // 导出excel
    getExcel() {
      require.ensure([], () => {
        const { export_json_to_excel } = require("../../excel/Export2Excel.js");
        const tHeader = ["姓名", "年龄"];
        const filterVal = ["name", "age"];
        const list = [
          { name: "小白", age: "18" },
          { name: "小黑", age: "16" },
        ];
        const data = this.formatJson(filterVal, list);
        export_json_to_excel(tHeader, data, "班级管理");
      });
    },

    formatJson(filterVal, jsonData) {
      return jsonData.map((v) => filterVal.map((j) => v[j]));
    },
  },
  beforeCreate(){
    service({
        method:"get",
        url:"/grade"
      }).then((res)=>{
        console.log("请求成功",res)
        this.tableData=res.data.users
        this.searchTable = this.tableData;
      },(error)=>{
        console.log(error)
      })
  },
  mounted() {
    console.log(1);
    // 加载完成后给sertchtable赋值 页面显示searchtable数据
    // this.searchTable = this.tableData;
  },
};
</script>

<style scoped>
.el-input {
  width: 278px;
  /* display: block; */
  margin: 12px 12px 12px 0;
}

</style>